package nachos.threads;

import nachos.machine.*;

/**
 * An implementation of condition variables that disables interrupt()s for
 * synchronization.
 *
 * <p>
 * You must implement this.
 *
 * @see	nachos.threads.Condition
 */
public class Condition2 {
    /**
     * Allocate a new condition variable.
     *
     * @param	conditionLock	the lock associated with this condition
     *				variable. The current thread must hold this
     *				lock whenever it uses <tt>sleep()</tt>,
     *				<tt>wake()</tt>, or <tt>wakeAll()</tt>.
     */
    public Condition2(Lock conditionLock) {
	this.conditionLock = conditionLock;
    /*************************/
	waitQueue = ThreadedKernel.scheduler.newThreadQueue(false);
	//waitQueue.acquire(thread);
    /*************************/
    }

    /**
     * Atomically release the associated lock and go to sleep on this condition
     * variable until another thread wakes it using <tt>wake()</tt>. The
     * current thread must hold the associated lock. The thread will
     * automatically reacquire the lock before <tt>sleep()</tt> returns.
     */
    public void sleep() {
	Lib.assertTrue(conditionLock.isHeldByCurrentThread());
	if(null_wake!=0)
	{
		null_wake--;
		return ;
	}
	conditionLock.release();
    /*************************/
	boolean status = Machine.interrupt().disable();
	KThread kt = KThread.currentThread();
	waitQueue.waitForAccess(kt);
	kt.sleep();
	wait_count++;
	Machine.interrupt().restore(status);
    /*************************/
	conditionLock.acquire();
    }

    /**
     * Wake up at most one thread sleeping on this condition variable. The
     * current thread must hold the associated lock.
     */
    public void wake() {
	Lib.assertTrue(conditionLock.isHeldByCurrentThread());
    /*************************/
	boolean status = Machine.interrupt().disable();
	KThread kt = waitQueue.nextThread();

	if(kt!=null)
	{
		kt.ready();
		wait_count--;
	}
	else
	{
		null_wake++;
	}
	Machine.interrupt().restore(status);
    /*************************/
    }

    /**
     * Wake up all threads sleeping on this condition variable. The current
     * thread must hold the associated lock.
     */
    public void wakeAll() {
	Lib.assertTrue(conditionLock.isHeldByCurrentThread());
    /*************************/
	boolean status = Machine.interrupt().disable();
	System.out.println("wait_count:"+wait_count);
	while(wait_count!=0)
	{
		wake();
	}
	Machine.interrupt().restore(status);
    /*************************/
    }
    
    private Lock conditionLock;
    /*************************/
    private ThreadQueue waitQueue;
    private int wait_count;
	int null_wake = 0;
    /*************************/
}
